﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.DataAccessProvider;
using DTN.Core;

namespace Hanel.DTN.News
{
    public class tblCommentManager : ItblCommentManager
    {
        private static ItblCommentManager itblCommentManager = null;
         private tblCommentManager()
        {
        }
         public static ItblCommentManager CreateInstance()
         {
             if (itblCommentManager == null)
             {
                 itblCommentManager = new tblCommentManager();
             }
             return itblCommentManager;
         }
         public int InserttblComment(tblComment tblComment)
         {
             DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
             int inumOfRowEffect = 0;
             SqlParameter[] parameters = new SqlParameter[]{
                                                                new SqlParameter("@ID", SqlDbType.Int),
                                                                new SqlParameter("@content_ID", SqlDbType.Int),																															
                                                                new SqlParameter("@HoTen", SqlDbType.NVarChar,50),
                                                                new SqlParameter("@Email", SqlDbType.NVarChar,50),
                                                                new SqlParameter("@TieuDe", SqlDbType.NVarChar,100),
                                                                new SqlParameter("@NoiDung", SqlDbType.NVarChar,4000),
                                                                new SqlParameter("@com_create", SqlDbType.DateTime),
                                                                new SqlParameter("@active", SqlDbType.Bit),
                                                               

                                                            };
             parameters[0].Direction = ParameterDirection.Output;
             parameters[1].Value = tblComment.Content_ID;
             parameters[2].Value = tblComment.HoTen;
             parameters[3].Value = tblComment.Email;
             parameters[4].Value = tblComment.TieuDe;
             parameters[5].Value = tblComment.NoiDung;
             parameters[6].Value = tblComment.com_create;
             parameters[7].Value = tblComment.active;
            
             try
             {
                 inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_InserttblComment", CommandType.StoredProcedure, parameters);
                 tblComment.ID = Convert.ToInt32(parameters[0].Value);
             }
             catch (Exception ex)
             {
                 tblComment.ID = -1;
             }
             return tblComment.ID;
         }

         public bool UpdatetblComment(tblComment tblComment)
         {
             bool bresult = false;
             DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
             int inumOfRowEffect = 0;
             SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
                                                                new SqlParameter("@content_ID", SqlDbType.Int),																															
                                                                new SqlParameter("@HoTen", SqlDbType.NVarChar,50),
                                                                new SqlParameter("@Email", SqlDbType.NVarChar,50),
                                                                new SqlParameter("@TieuDe", SqlDbType.NVarChar,100),
                                                                new SqlParameter("@NoiDung", SqlDbType.NVarChar,4000),
                                                                new SqlParameter("@com_create", SqlDbType.DateTime),
                                                                new SqlParameter("@active", SqlDbType.Bit),
															};
             parameters[0].Value = tblComment.ID;
             parameters[1].Value = tblComment.Content_ID;
             parameters[2].Value = tblComment.HoTen;
             parameters[3].Value = tblComment.Email;
             parameters[4].Value = tblComment.TieuDe;
             parameters[5].Value = tblComment.NoiDung;
             parameters[6].Value = tblComment.com_create;
             parameters[7].Value = tblComment.active;

             try
             {
                 inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_UpdatetblComment", CommandType.StoredProcedure, parameters);
                 bresult = true;
             }
             catch (Exception ex)
             {
                 bresult = false;
             }
             return bresult;
         }

         //Delete
         public bool DeletetblComment(int id)
         {
             bool bresult = false;
             DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
             int inumOfRowEffect = 0;
             SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
															};
             parameters[0].Value = id;
             try
             {
                 inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_DeletetblComment", CommandType.StoredProcedure, parameters);
                 bresult = true;
             }
             catch (Exception ex)
             {
                 bresult = false;
             }
             return bresult;
         }

         public DataTable SelecttblCommentDynamic(string Where, string Order)
         {
             DataSet ds = new DataSet();
             DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
             SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@WhereCondition", SqlDbType.NVarChar,500),
																new SqlParameter("@OrderByExpression", SqlDbType.NVarChar,250),
															};
             parameters[0].Value = Where;
             parameters[1].Value = Order;
             try
             {
                 ds = dataProvider.ExecuteDataSet("sp_SelecttblCommentDynamic", CommandType.StoredProcedure, parameters);
             }
             catch (Exception ex)
             {
                 return null;
             }
             return ds.Tables["Table"];
         }
         public DataTable SelecttblCommentDynamic_top(string Top, string Where, string Order)
         {
             DataSet ds = new DataSet();
             DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
             SqlParameter[] parameters = new SqlParameter[]{
                                                                new SqlParameter("@Top", SqlDbType.NVarChar,20),
																new SqlParameter("@WhereCondition", SqlDbType.NVarChar,500),
																new SqlParameter("@OrderByExpression", SqlDbType.NVarChar,250),
															};
             parameters[0].Value = Top;
             parameters[1].Value = Where;
             parameters[2].Value = Order;
             try
             {
                 ds = dataProvider.ExecuteDataSet("sp_SelecttblCommentDynamic_Top", CommandType.StoredProcedure, parameters);
             }
             catch (Exception ex)
             {
                 return null;
             }
             return ds.Tables["Table"];
         }
    }
}
